package com.hopu.v1.controller.front.question;

import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.question.ErrorNotebooksQuestions;
import com.hopu.v1.pojo.question.Questions;
import com.hopu.v1.pojo.question.QuestionsOptions;
import com.hopu.v1.service.front.question.FQuestionsService;

import com.hopu.v1.utils.JwtUtils;
import com.hopu.v1.vo.JudgeQuestionVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 题库管理
 */
@RestController
@RequestMapping("/fquestion")
public class FQuestionController {

    @Autowired
    private FQuestionsService fQuestionService;

    //获取开始刷题的错题题目
    @GetMapping("/getErrQuestions")
    public R getErrQuestions(String studentId, HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println("token = " + token);
        List<ErrorNotebooksQuestions> questions = fQuestionService.queryErrQuestions(studentId, 3);
        if (questions==null || questions.size()==0){
            return R.err("您已经更正所有错题了，暂时没有错题！！！");
        }
        return R.ok(questions);
    }

    //判断题目对错
    @PostMapping("/judgeQuestion")
    public R judgeQuestion(Integer questionId,String answer, HttpServletRequest request){
        String token = request.getHeader("frontToken");
        String studentId = JwtUtils.getUserId(token);
        JudgeQuestionVo judgeQuestionVo = fQuestionService.judgeQuestion(studentId, questionId, answer);
        if (judgeQuestionVo==null){
            return R.err("非法入侵，请遵守法律");
        }else {
            return R.ok(judgeQuestionVo.getMessage(),judgeQuestionVo.getQuestions());
        }
    }


    //获取开始刷题的题目
    @GetMapping("/getQuestions")
    public R getQuestions(int[] ids,Integer subjectId){
        if (subjectId==null || subjectId==0){
            if (ids==null || ids.length==0)
                return R.err("非法入侵，请遵守法律");
        }
        if (ids==null || ids.length==0){
            List<Questions> questions = fQuestionService.queryQuestions(null, 0,subjectId);
            //清空返回选项的数据状态
            for (Questions question : questions) {
                for (QuestionsOptions option : question.getOptions()) {
                    option.setStatus(0);
                }
            }
            return R.ok(questions);
        }
        List<Questions> questions = fQuestionService.queryQuestions(ids, 10,subjectId);
        if (questions==null || questions.size()==0){
            return R.err("目前题库还在更新，暂时没有题目");
        }
        if (questions.get(0).getQuestionTitle()==null){
            return R.ok("题库编号id为"+questions.get(0).getQuestionBanksId()+",目前没有题目",questions);
        }
        //清空返回选项的数据状态
        for (Questions question : questions) {
            for (QuestionsOptions option : question.getOptions()) {
                option.setStatus(0);
            }
        }
        return R.ok(questions);
    }

    //获取所有题目
    @GetMapping("/getQuestionList")
    public R getQuestionList(Integer id,Integer status){
        if (id==null)
            return R.err("非法入侵，请遵守法律");
        List<Questions> questions = fQuestionService.queryQuestionList(id,status);
        return R.ok(questions);
    }

    @GetMapping("/selectStudentAndComment")
    public R selectStudentAndComment(Questions questions){
        return R.ok(fQuestionService.selectStudentAndComment(questions));
    }

    @GetMapping("/selectStudentComment")
    public R selectStudentComment(Questions questions){
        return R.ok(fQuestionService.selectStudentComment(questions));
    }

}
